package com.liukai.novel_rack.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.liukai.novel_commons.entities.NovelInfo;
import com.liukai.novel_commons.utils.Constans;
import com.liukai.novel_rack.R;
import com.liukai.novel_rack.search_interface.OnSearchItemListener;

import java.util.ArrayList;
import java.util.List;

public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.HistoryHolder> {

    private List<NovelInfo> historyNovelList = new ArrayList<>();

    public HistoryAdapter() {
        this.historyNovelList = Constans.getHistoryNovelList();
    }

    @NonNull
    @Override
    public HistoryHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_history, null);
        return new HistoryHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull HistoryHolder holder, final int position) {
        holder.mTvNovelName.setText(historyNovelList.get(position).getNovelName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnSearchItemListener.onItemClick(historyNovelList.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return historyNovelList.size();
    }

    class HistoryHolder extends RecyclerView.ViewHolder {

        public TextView mTvNovelName;

        public HistoryHolder(@NonNull View itemView) {
            super(itemView);
            mTvNovelName = itemView.findViewById(R.id.novel_name);
        }
    }

    private OnSearchItemListener mOnSearchItemListener;

    public void setOnSearchItemListener(OnSearchItemListener onSearchItemListener) {
        mOnSearchItemListener = onSearchItemListener;
    }
}
